package com.kakarote.crm9.erp.jxc.controller;

import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.core.paragetter.Para;
import com.jfinal.plugin.activerecord.Record;
import com.kakarote.crm9.erp.jxc.entity.GoodsSpecification;
import com.kakarote.crm9.erp.jxc.service.GoodsSpecificationService;
import com.kakarote.crm9.utils.BaseUtil;
import com.kakarote.crm9.utils.PageData;
import com.kakarote.crm9.utils.R;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author xjl
 * @date 2021/3/15 16:53
 */
public class GoodsSpecificationController extends Controller {

    @Inject
    private GoodsSpecificationService specificationService;

    /**
     * 查询所有商品单位
     */
    public void queryList() {
        PageData requestData = getRequestData();
        List<Record> records = specificationService.queryList2(requestData);
        renderJson(records == null ? R.error() : R.ok().put("data", records).put("total", specificationService.queryCount().getInt("count")));
    }

    /**
     * 根据id查询
     * @param id
     */
    public void queryId(@Para("id") Integer id) {
        renderJson(R.ok().put("data", specificationService.queryById(id)));
    }

    /**
     * 添加
     */
    public void add() {
        PageData requestData = getRequestData();
        GoodsSpecification goodsSpecification = new GoodsSpecification();

        goodsSpecification.setCategorieId(requestData.get("categorieId", Integer.class));
        goodsSpecification.setSpecificationName(requestData.getString("specificationName"));
        goodsSpecification.setPrice(requestData.get("price", BigDecimal.class));
        goodsSpecification.setCreateTime(new Date());
        goodsSpecification.setCreateBy(BaseUtil.getUser().getUsername());

        Record record = specificationService.queryNameAndType(goodsSpecification.getCategorieId(), goodsSpecification.getSpecificationName());

        renderJson(record != null ? R.error("此规格已存在") : specificationService.insertSpecification(goodsSpecification));
    }

    /**
     * 更新
     * @param goodsSpecification
     */
    public void update(@Para("") GoodsSpecification goodsSpecification) {
        if (goodsSpecification == null) renderJson(R.error());
        renderJson(specificationService.updateSpecification(goodsSpecification));
    }

    /**
     * 删除
     */
    public void delete(@Para("ids") String ids) {
        if (ids == null || ids.length() <= 0) renderJson(R.error());
        if (ids.contains(",")) {
            String[] split = ids.split(",");
            int[] params = new int[split.length];
            for (int i = 0; i < split.length; i++) {
                params[i] = Integer.parseInt(split[i]);
            }
            renderJson(specificationService.deleteSpecification(params));
        } else {
            renderJson(specificationService.deleteSpecification(new int[] { Integer.parseInt(ids) }));
        }

    }

    /**
     * 获取数据并封装成map
     * @return
     */
    private PageData getRequestData() {
        return new PageData(this.getRequest());
    }
}
